import {
    baseUrl
} from '@/common/unit.js'

export const uploadImage = (count = 1) => {
    return new Promise((resolve, reject) => {
        let sourceType = ['album', 'camera'];
        uni.chooseImage({
            count,
            sourceType,
            success: async (res) => {
                uni.showLoading({
                    title: '上传中...'
                })
                let n = 0;
                let arr = [];
                let {
                    tempFilePaths,
                    tempFiles
                } = res;
                for (let i = 0; i < tempFilePaths.length; i++) {
                    uni.uploadFile({
                        url: baseUrl + '/user/uploadFile/uploadFile',
                        filePath: tempFilePaths[i],
                        name: 'file',
                        success: (res) => {
                            let {
                                data
                            } = res;
                            data = JSON.parse(data);
                            arr.push(data.data);
                            n++;
                            if (n === tempFilePaths.length) {
                                resolve(arr && arr.length === 1 ? arr[0] : arr);
                                uni.hideLoading();
                            }
                        },
                        fail: (err) => {
                            reject(err);
                            uni.hideLoading();
                            uni.showToast({
                                icon: 'none',
                                title: '上传失败'
                            })
                        }
                    })
                }

            },
            fail: (err) => {

            }
        });
    })
}

export const uploadImg = (filePath) => {
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: baseUrl + '/user/uploadFile/uploadFile',
            filePath,
            name: 'file',
            success: (res) => {
                let {
                    data
                } = res;
                data = JSON.parse(data);
                resolve(data.data);
            },
            fail: (err) => {
                reject(err);
                uni.showToast({
                    icon: 'none',
                    title: '上传失败'
                })
            }
        })
    })
}
export const upFile = (itineraryId) => {
    return new Promise((resolve, reject) => {
        // #ifdef H5
        uni.chooseFile({
            count: 1,
            type: 'file',
            success(res) {
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = res.tempFiles;
                uni.uploadFile({
                    url: baseUrl + '/plan/proposalItinerary/importPartyExcel',
                    filePath: tempFilePaths[0].path, //选择单个路径
                    name: 'file', //后端需要拿到这个Key来获取二进制文件内容
                    header: {
                        'Authentication': uni.getStorageSync('userinfo').authentication
                    },
                    formData: {
                        itineraryId
                    },
                    success: (uploadFileRes) => {
                        //接口返回的数据
                        console.log(uploadFileRes.data);
                        //接口返回的状态码
                        let data = JSON.parse(uploadFileRes.data);
                        resolve(data)
                    }
                });
            }
        })
        // #endif

        // #ifdef MP-WEIXIN
        wx.chooseMessageFile({
            count: 1, //限制选择的文件数量
            type: 'file', //非图片和视频的文件,不选默认为all
            success(res) {
                const tempFilePaths = res.tempFiles
                uni.uploadFile({
                    url: baseUrl + '/plan/proposalItinerary/importPartyExcel',
                    filePath: tempFilePaths[0].path, //选择单个路径
                    name: 'file', //后端需要拿到这个Key来获取二进制文件内容
                    header: {
                        'Authentication': uni.getStorageSync('userinfo').authentication
                    },
                    formData: {
                        itineraryId
                    },
                    success: (uploadFileRes) => {
                        //接口返回的数据
                        let data = JSON.parse(uploadFileRes.data);
                        resolve(data)
                    }
                });

            }
        })
        // #endif


    })
}


export const ImportingPersonnel = (uniKey) => {
    return new Promise((resolve, reject) => {
        // #ifdef H5
        uni.chooseFile({
            count: 1,
            type: 'file',
            success(res) {
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = res.tempFiles;
                uni.uploadFile({
                    url: baseUrl + '/plan/proposalItinerary/importPartyExcel',
                    filePath: tempFilePaths[0].path, //选择单个路径
                    name: 'file', //后端需要拿到这个Key来获取二进制文件内容
                    header: {
                        'Authentication': uni.getStorageSync('userinfo').authentication
                    },
                    formData: {
                        uniKey
                    },
                    success: (uploadFileRes) => {
                        //接口返回的数据
                        console.log(uploadFileRes.data);
                        //接口返回的状态码
                        let data = JSON.parse(uploadFileRes.data);
                        resolve(data)
                    }
                });
            }
        })
        // #endif

        // #ifdef MP-WEIXIN
        wx.chooseMessageFile({
            count: 1, //限制选择的文件数量
            type: 'file', //非图片和视频的文件,不选默认为all
            success(res) {
                const tempFilePaths = res.tempFiles
                uni.uploadFile({
                    url: baseUrl + '/plan/proposalItinerary/importPartyExcel',
                    filePath: tempFilePaths[0].path, //选择单个路径
                    name: 'file', //后端需要拿到这个Key来获取二进制文件内容
                    header: {
                        'Authentication': uni.getStorageSync('userinfo').authentication
                    },
                    formData: {
                        uniKey
                    },
                    success: (uploadFileRes) => {
                        //接口返回的数据
                        let data = JSON.parse(uploadFileRes.data);
                        resolve(data)
                    }
                });

            }
        })
        // #endif


    })
}
